package com.tech.arno.spicy.ui.view;

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tech.arno.spicy.R;
import com.tech.arno.spicy.module.recommand.RecommandFooterValue;
import com.tech.arno.spicy.module.recommand.RecommandHeadValue;
import com.tech.arno.spicy.ui.adapter.PhotoPagerAdapter;
import com.tech.arno.spicy.util.ImageLoaderManager;
import com.viewpagerindicator.CirclePageIndicator;

import cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager;

/**
 * Created by Arno on 2017/12/10 0010.
 */

public class HomeHeaderView extends ConstraintLayout {

    private Context mContext;


    /*
    * UI
    * */

    private ConstraintLayout mRootView;
    private AutoScrollViewPager mViewPager;
    private CirclePageIndicator mPagerIndicator;
    private TextView mHotView;
    private PhotoPagerAdapter mAdapter;
    private ImageView[] mImageViews = new ImageView[4];
    private LinearLayout mFooterLayout;

    /*
    * Data
    * */

    private RecommandHeadValue mHeaderValue;
    private ImageLoaderManager mImageLoader;


    public HomeHeaderView(Context context, RecommandHeadValue headerValue) {
        this(context, null, headerValue);
    }

    public HomeHeaderView(Context context, AttributeSet attrs, RecommandHeadValue headerValue) {
        super(context, attrs);
        mContext = context;
        mHeaderValue = headerValue;
        mImageLoader = ImageLoaderManager.getInstance(mContext);
        initView();
    }

    private void initView() {
        LayoutInflater inflater = LayoutInflater.from(mContext);

        mRootView = (ConstraintLayout) inflater.inflate(R.layout.header_home_layout, this);
        mViewPager = mRootView.findViewById(R.id.pager);
        mPagerIndicator = mRootView.findViewById(R.id.pager_indicator_view);
        mHotView = mRootView.findViewById(R.id.zuixing_view);
        mImageViews[0] = mRootView.findViewById(R.id.head_image_one);
        mImageViews[1] = mRootView.findViewById(R.id.head_image_two);
        mImageViews[2] = mRootView.findViewById(R.id.head_image_three);
        mImageViews[3] = mRootView.findViewById(R.id.head_image_four);

        mFooterLayout = mRootView.findViewById(R.id.content_layout);
//为View填充数据
        mAdapter = new PhotoPagerAdapter(mContext, mHeaderValue.ads, true);
        mViewPager.setAdapter(mAdapter);
        mViewPager.startAutoScroll(5000);
        mPagerIndicator.setViewPager(mViewPager);
        mPagerIndicator.setFillColor(R.color.possible_result_points);

        for (int i = 0; i < mImageViews.length; i++) {
            mImageLoader.displayImage(mImageViews[i], mHeaderValue.middle.get(i));
        }
        mHotView.setText("Arno Spicy Demo");
        for (RecommandFooterValue value : mHeaderValue.footer) {
            mFooterLayout.addView(createItem(value));
        }


    }

    private RecommandBottomItem createItem(RecommandFooterValue value) {
        RecommandBottomItem item = new RecommandBottomItem(mContext, value);
        return item;
    }


}
